package org.gpp.proj1.ui.gui.profile;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import org.gpp.proj1.ui.gui.SRGGUIView;

public class CreateProfileFrame extends JDialog implements ActionListener {

	private static final long serialVersionUID = -982519204978151913L;

	private static final String CLOSE_WINDOW = "closeWindow";

	private static final String CREATE_PROFILE = "createProfile";

	private SRGGUIView view;

	private javax.swing.JButton cancelButton;

	private javax.swing.JButton createProfileButton;

	private javax.swing.JLabel nameLabel;

	private javax.swing.JTextField profileName;

	public CreateProfileFrame(SRGGUIView view) {
		this.view = view;
		this.setModal(true);
		
		this.initComponents();
		
		this.setLocationRelativeTo( null );
	}

	private void initComponents() {
		nameLabel = new javax.swing.JLabel();
		profileName = new javax.swing.JTextField();
		cancelButton = new javax.swing.JButton();
		createProfileButton = new javax.swing.JButton();

		this.setTitle( "Create profile" );
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE );
		this.setResizable( false );
		
		nameLabel.setText("Profile name:");

		profileName.setText("Name");

		cancelButton.setText("Cancel");
		cancelButton.addActionListener( this );
		cancelButton.setActionCommand( CLOSE_WINDOW );

		createProfileButton.setText("Create");
		createProfileButton.addActionListener( this );
		createProfileButton.setActionCommand( CREATE_PROFILE );

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.addContainerGap()
						.add(nameLabel)
						.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
						.add(profileName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 207, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
								.addContainerGap(157, Short.MAX_VALUE)
								.add(createProfileButton)
								.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
								.add(cancelButton)
								.addContainerGap())
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.addContainerGap()
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
								.add(nameLabel)
								.add(profileName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
								.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
										.add(cancelButton)
										.add(createProfileButton))
										.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		pack();
	}    

	public static void display(SRGGUIView view) {
		CreateProfileFrame frame = new CreateProfileFrame( view );
		frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if( e.getActionCommand().equals( CLOSE_WINDOW ) ) {
			this.dispose();
		} else {
			try { 
				view.getController().createPlayerProfile( profileName.getText() );
				view.refreshProfiles();
			} catch( IllegalArgumentException iae ) {
				JOptionPane.showMessageDialog( null, "Could not create" +
						"Profile: \"" + iae.getMessage() + "\"", "Error", JOptionPane.ERROR_MESSAGE );
			}
			this.dispose();
		}		
	}
}
